package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.mapper.QuestionMapper;
import cn.wolfcode.trip.base.query.QuestionQueryObject;
import cn.wolfcode.trip.base.service.IQuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public PageInfo selectQuestions(QuestionQueryObject qo) {
        if (qo.getOrderBy()!=null) {
            PageHelper.startPage(qo.getCurrentPage(),3,qo.getOrderBy());
        }else{
            PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        }
        List<Question> list = questionMapper.selectQuestions(qo);
        return new PageInfo(list);
    }

    @Override
    public Question selectById(Long id) {

        return questionMapper.selectByPrimaryKey(id);
    }

    @Override
    public void changeState(Long questionId) {
        questionMapper.changeState(questionId);
    }

    @Override
    public void saveQuestion(Question question) {
        question.setState(Question.STATE_UNSOLVED);
        question.setCreateTime(new Date());
        questionMapper.insert(question);
    }

    @Override
    public void updateViewCount(Long id) {
        questionMapper.updateViewCount(id);
    }
}
